{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "173939c0",
   "metadata": {},
   "source": [
    "### P067 数据降维PCA-标准化输入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "63843485",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c6b5442e",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cb9f4652",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./pca.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "686426c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>var1</th>\n",
       "      <th>var2</th>\n",
       "      <th>var3</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   var1  var2  var3  class\n",
       "0   5.1   1.4   0.2    0.0\n",
       "1   4.9   1.4   0.2    0.0\n",
       "2   4.7   1.3   0.2    0.0\n",
       "3   4.6   1.5   0.2    0.0\n",
       "4   5.0   1.4   0.2    0.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f4b3f553",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "17f51dd7",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = X.pop(\"class\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4cb2f6af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>var1</th>\n",
       "      <th>var2</th>\n",
       "      <th>var3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   var1  var2  var3\n",
       "0   5.1   1.4   0.2\n",
       "1   4.9   1.4   0.2\n",
       "2   4.7   1.3   0.2\n",
       "3   4.6   1.5   0.2\n",
       "4   5.0   1.4   0.2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "17d3ddbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.0\n",
       "1    0.0\n",
       "2    0.0\n",
       "3    0.0\n",
       "4    0.0\n",
       "Name: class, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c5239d07",
   "metadata": {},
   "outputs": [],
   "source": [
    "scaler = StandardScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0d122f05",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_std = scaler.fit_transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "829f5a8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.90068117, -1.34022653, -1.3154443 ],\n",
       "       [-1.14301691, -1.34022653, -1.3154443 ],\n",
       "       [-1.38535265, -1.39706395, -1.3154443 ],\n",
       "       [-1.50652052, -1.2833891 , -1.3154443 ],\n",
       "       [-1.02184904, -1.34022653, -1.3154443 ],\n",
       "       [-0.53717756, -1.16971425, -1.05217993],\n",
       "       [-1.50652052, -1.34022653, -1.18381211],\n",
       "       [-1.02184904, -1.2833891 , -1.3154443 ],\n",
       "       [-1.74885626, -1.34022653, -1.3154443 ],\n",
       "       [-1.14301691, -1.2833891 , -1.44707648]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_std[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87487c85",
   "metadata": {},
   "source": [
    "### P068 数据降维PCA-自己实现PCA算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e9915fc8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.90068117, -1.34022653, -1.3154443 ],\n",
       "       [-1.14301691, -1.34022653, -1.3154443 ],\n",
       "       [-1.38535265, -1.39706395, -1.3154443 ],\n",
       "       [-1.50652052, -1.2833891 , -1.3154443 ],\n",
       "       [-1.02184904, -1.34022653, -1.3154443 ],\n",
       "       [-0.53717756, -1.16971425, -1.05217993],\n",
       "       [-1.50652052, -1.34022653, -1.18381211],\n",
       "       [-1.02184904, -1.2833891 , -1.3154443 ],\n",
       "       [-1.74885626, -1.34022653, -1.3154443 ],\n",
       "       [-1.14301691, -1.2833891 , -1.44707648]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_std[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b7b20472",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.00671141, 0.87760447, 0.82343066],\n",
       "       [0.87760447, 1.00671141, 0.96932762],\n",
       "       [0.82343066, 0.96932762, 1.00671141]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cov = np.cov(X_std, rowvar=False)\n",
    "cov"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bf079910",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([2.78833033, 0.20075012, 0.03105378]),\n",
       " array([[ 0.55964149,  0.81270446,  0.16221241],\n",
       "        [ 0.59148855, -0.2546058 , -0.76506024],\n",
       "        [ 0.58046765, -0.52410624,  0.62319335]]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eig_vals, eig_vecs = np.linalg.eig(cov)\n",
    "eig_vals, eig_vecs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1f4cf3e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(2.7883303296752926, array([0.55964149, 0.59148855, 0.58046765])),\n",
       " (0.20075011806343787, array([ 0.81270446, -0.2546058 , -0.52410624])),\n",
       " (0.031053780449189813, array([ 0.16221241, -0.76506024,  0.62319335]))]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eig_pairs = [\n",
    "    (\n",
    "        np.abs(eig_vals[i]),\n",
    "        eig_vecs[:, i]\n",
    "    )\n",
    "    for i in range(len(eig_vals))\n",
    "]\n",
    "eig_pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "53967800",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(2.7883303296752926, array([0.55964149, 0.59148855, 0.58046765])),\n",
       " (0.20075011806343787, array([ 0.81270446, -0.2546058 , -0.52410624])),\n",
       " (0.031053780449189813, array([ 0.16221241, -0.76506024,  0.62319335]))]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eig_pairs.sort(reverse=True)\n",
    "eig_pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c39b4203",
   "metadata": {},
   "outputs": [],
   "source": [
    "W = np.hstack(\n",
    "    (\n",
    "        eig_pairs[0][1].reshape(3, 1),\n",
    "        eig_pairs[1][1].reshape(3, 1),\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "02f64439",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.55964149,  0.81270446],\n",
       "       [ 0.59148855, -0.2546058 ],\n",
       "       [ 0.58046765, -0.52410624]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "W"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f816380d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.06036006,  0.2986744 ],\n",
       "       [-2.1959812 ,  0.10172707],\n",
       "       [-2.36522102, -0.08074913],\n",
       "       [-2.36579421, -0.20816508],\n",
       "       [-2.12817063,  0.20020073],\n",
       "       [-1.60325585,  0.4127035 ],\n",
       "       [-2.32300467, -0.26268319],\n",
       "       [-2.09455194,  0.1857296 ],\n",
       "       [-2.53503403, -0.39064128],\n",
       "       [-2.23877073,  0.15624518]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_pca = X_std.dot(W)\n",
    "X_pca[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f56daea9",
   "metadata": {},
   "source": [
    "### P069 数据降维PCA-组合降维结果和标签数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "81808ac5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.06036006,  0.2986744 ],\n",
       "       [-2.1959812 ,  0.10172707],\n",
       "       [-2.36522102, -0.08074913],\n",
       "       [-2.36579421, -0.20816508],\n",
       "       [-2.12817063,  0.20020073],\n",
       "       [-1.60325585,  0.4127035 ],\n",
       "       [-2.32300467, -0.26268319],\n",
       "       [-2.09455194,  0.1857296 ],\n",
       "       [-2.53503403, -0.39064128],\n",
       "       [-2.23877073,  0.15624518]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_pca[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d5c58fee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.060360</td>\n",
       "      <td>0.298674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.195981</td>\n",
       "      <td>0.101727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.365221</td>\n",
       "      <td>-0.080749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.365794</td>\n",
       "      <td>-0.208165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.128171</td>\n",
       "      <td>0.200201</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      pca_1     pca_2\n",
       "0 -2.060360  0.298674\n",
       "1 -2.195981  0.101727\n",
       "2 -2.365221 -0.080749\n",
       "3 -2.365794 -0.208165\n",
       "4 -2.128171  0.200201"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pca = pd.DataFrame(\n",
    "    data=X_pca,\n",
    "    columns = [\"pca_1\", \"pca_2\"]\n",
    ")\n",
    "df_pca.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "294093e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_pca[\"class\"] = df[\"class\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "50da680a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.060360</td>\n",
       "      <td>0.298674</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.195981</td>\n",
       "      <td>0.101727</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.365221</td>\n",
       "      <td>-0.080749</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.365794</td>\n",
       "      <td>-0.208165</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.128171</td>\n",
       "      <td>0.200201</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      pca_1     pca_2  class\n",
       "0 -2.060360  0.298674    0.0\n",
       "1 -2.195981  0.101727    0.0\n",
       "2 -2.365221 -0.080749    0.0\n",
       "3 -2.365794 -0.208165    0.0\n",
       "4 -2.128171  0.200201    0.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pca.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45c15926",
   "metadata": {},
   "source": [
    "### P070 数据降维PCA-使用sklearn的pca算法\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "af0b7215",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.90068117, -1.34022653, -1.3154443 ],\n",
       "       [-1.14301691, -1.34022653, -1.3154443 ],\n",
       "       [-1.38535265, -1.39706395, -1.3154443 ],\n",
       "       [-1.50652052, -1.2833891 , -1.3154443 ],\n",
       "       [-1.02184904, -1.34022653, -1.3154443 ],\n",
       "       [-0.53717756, -1.16971425, -1.05217993],\n",
       "       [-1.50652052, -1.34022653, -1.18381211],\n",
       "       [-1.02184904, -1.2833891 , -1.3154443 ],\n",
       "       [-1.74885626, -1.34022653, -1.3154443 ],\n",
       "       [-1.14301691, -1.2833891 , -1.44707648]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_std[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "769b6202",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "fe46fa09",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = PCA(n_components=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a57a3ff5",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_pca = pca.fit_transform(X_std)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cb4c336a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.06036006, -0.2986744 ],\n",
       "       [-2.1959812 , -0.10172707],\n",
       "       [-2.36522102,  0.08074913],\n",
       "       [-2.36579421,  0.20816508],\n",
       "       [-2.12817063, -0.20020073],\n",
       "       [-1.60325585, -0.4127035 ],\n",
       "       [-2.32300467,  0.26268319],\n",
       "       [-2.09455194, -0.1857296 ],\n",
       "       [-2.53503403,  0.39064128],\n",
       "       [-2.23877073, -0.15624518]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_pca[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9feb0e8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.060360</td>\n",
       "      <td>-0.298674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.195981</td>\n",
       "      <td>-0.101727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.365221</td>\n",
       "      <td>0.080749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.365794</td>\n",
       "      <td>0.208165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.128171</td>\n",
       "      <td>-0.200201</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      pca_1     pca_2\n",
       "0 -2.060360 -0.298674\n",
       "1 -2.195981 -0.101727\n",
       "2 -2.365221  0.080749\n",
       "3 -2.365794  0.208165\n",
       "4 -2.128171 -0.200201"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(\n",
    "    data = X_pca,\n",
    "    columns =[\"pca_1\", \"pca_2\"]\n",
    ")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "77fd7b01",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"class\"] = y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "103e5f69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pca_1</th>\n",
       "      <th>pca_2</th>\n",
       "      <th>class</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-2.060360</td>\n",
       "      <td>-0.298674</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-2.195981</td>\n",
       "      <td>-0.101727</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-2.365221</td>\n",
       "      <td>0.080749</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.365794</td>\n",
       "      <td>0.208165</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.128171</td>\n",
       "      <td>-0.200201</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      pca_1     pca_2  class\n",
       "0 -2.060360 -0.298674    0.0\n",
       "1 -2.195981 -0.101727    0.0\n",
       "2 -2.365221  0.080749    0.0\n",
       "3 -2.365794  0.208165    0.0\n",
       "4 -2.128171 -0.200201    0.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7e8dddc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f1e7e4aa",
   "metadata": {},
   "source": [
    "### P071 数据降维PCA - 计算观察方差分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "833405d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "bd4a63c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b318c7a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"./p071.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d1936384",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>var_1</th>\n",
       "      <th>var_2</th>\n",
       "      <th>var_3</th>\n",
       "      <th>var_4</th>\n",
       "      <th>var_5</th>\n",
       "      <th>var_6</th>\n",
       "      <th>var_7</th>\n",
       "      <th>var_8</th>\n",
       "      <th>var_9</th>\n",
       "      <th>var_10</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>14.23</td>\n",
       "      <td>1.71</td>\n",
       "      <td>2.43</td>\n",
       "      <td>15.6</td>\n",
       "      <td>127</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.06</td>\n",
       "      <td>0.28</td>\n",
       "      <td>2.29</td>\n",
       "      <td>5.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13.20</td>\n",
       "      <td>1.78</td>\n",
       "      <td>2.14</td>\n",
       "      <td>11.2</td>\n",
       "      <td>100</td>\n",
       "      <td>2.65</td>\n",
       "      <td>2.76</td>\n",
       "      <td>0.26</td>\n",
       "      <td>1.28</td>\n",
       "      <td>4.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>13.16</td>\n",
       "      <td>2.36</td>\n",
       "      <td>2.67</td>\n",
       "      <td>18.6</td>\n",
       "      <td>101</td>\n",
       "      <td>2.80</td>\n",
       "      <td>3.24</td>\n",
       "      <td>0.30</td>\n",
       "      <td>2.81</td>\n",
       "      <td>5.68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14.37</td>\n",
       "      <td>1.95</td>\n",
       "      <td>2.50</td>\n",
       "      <td>16.8</td>\n",
       "      <td>113</td>\n",
       "      <td>3.85</td>\n",
       "      <td>3.49</td>\n",
       "      <td>0.24</td>\n",
       "      <td>2.18</td>\n",
       "      <td>7.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13.24</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.87</td>\n",
       "      <td>21.0</td>\n",
       "      <td>118</td>\n",
       "      <td>2.80</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.39</td>\n",
       "      <td>1.82</td>\n",
       "      <td>4.32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   var_1  var_2  var_3  var_4  var_5  var_6  var_7  var_8  var_9  var_10\n",
       "0  14.23   1.71   2.43   15.6    127   2.80   3.06   0.28   2.29    5.64\n",
       "1  13.20   1.78   2.14   11.2    100   2.65   2.76   0.26   1.28    4.38\n",
       "2  13.16   2.36   2.67   18.6    101   2.80   3.24   0.30   2.81    5.68\n",
       "3  14.37   1.95   2.50   16.8    113   3.85   3.49   0.24   2.18    7.80\n",
       "4  13.24   2.59   2.87   21.0    118   2.80   2.69   0.39   1.82    4.32"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d28324ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 14.23,   1.71,   2.43,  15.6 , 127.  ,   2.8 ,   3.06,   0.28,\n",
       "          2.29,   5.64],\n",
       "       [ 13.2 ,   1.78,   2.14,  11.2 , 100.  ,   2.65,   2.76,   0.26,\n",
       "          1.28,   4.38],\n",
       "       [ 13.16,   2.36,   2.67,  18.6 , 101.  ,   2.8 ,   3.24,   0.3 ,\n",
       "          2.81,   5.68],\n",
       "       [ 14.37,   1.95,   2.5 ,  16.8 , 113.  ,   3.85,   3.49,   0.24,\n",
       "          2.18,   7.8 ],\n",
       "       [ 13.24,   2.59,   2.87,  21.  , 118.  ,   2.8 ,   2.69,   0.39,\n",
       "          1.82,   4.32],\n",
       "       [ 14.2 ,   1.76,   2.45,  15.2 , 112.  ,   3.27,   3.39,   0.34,\n",
       "          1.97,   6.75],\n",
       "       [ 14.39,   1.87,   2.45,  14.6 ,  96.  ,   2.5 ,   2.52,   0.3 ,\n",
       "          1.98,   5.25],\n",
       "       [ 14.06,   2.15,   2.61,  17.6 , 121.  ,   2.6 ,   2.51,   0.31,\n",
       "          1.25,   5.05],\n",
       "       [ 14.83,   1.64,   2.17,  14.  ,  97.  ,   2.8 ,   2.98,   0.29,\n",
       "          1.98,   5.2 ],\n",
       "       [ 13.86,   1.35,   2.27,  16.  ,  98.  ,   2.98,   3.15,   0.22,\n",
       "          1.85,   7.22]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = df.values\n",
    "data[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2f7a60f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "scaler = StandardScaler()\n",
    "data_std = scaler.fit_transform(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "487e3e73",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = PCA(n_components=3)\n",
    "data_pca = pca.fit_transform(data_std)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1f1a9919",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.76934698, -1.30493533, -0.58395762],\n",
       "       [ 1.67070079,  1.02905763, -1.81994227],\n",
       "       [ 2.10783949, -0.8316145 ,  0.99444906],\n",
       "       [ 3.44931605, -1.8592707 , -0.38935836],\n",
       "       [ 0.87603192, -1.50882534,  1.63982782],\n",
       "       [ 2.67119513, -1.29513525, -0.69468732],\n",
       "       [ 1.66955982, -0.57093005, -1.18079406],\n",
       "       [ 1.13000447, -1.39071555, -0.27443572],\n",
       "       [ 2.39875256, -0.27899794, -1.83738049],\n",
       "       [ 2.3741835 , -0.34132632, -1.10598146]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_pca[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "52f7794f",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = pd.DataFrame(\n",
    "    data = {\"variance_ratio\": pca.explained_variance_ratio_}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "f389ac8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "results[\"cumulative\"] = results[\"variance_ratio\"].cumsum()\n",
    "results[\"component\"] = results.index + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "58c0cf6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>variance_ratio</th>\n",
       "      <th>cumulative</th>\n",
       "      <th>component</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.338023</td>\n",
       "      <td>0.338023</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.199678</td>\n",
       "      <td>0.537701</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.133415</td>\n",
       "      <td>0.671117</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   variance_ratio  cumulative  component\n",
       "0        0.338023    0.338023          1\n",
       "1        0.199678    0.537701          2\n",
       "2        0.133415    0.671117          3"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50c4bcd5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1f196326",
   "metadata": {},
   "source": [
    "### P072 数据降维PCA - 指定方差百分比计算分量数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c3365c0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.51861254, -0.5622498 ,  0.23205254, -1.16959318,  1.91390522,\n",
       "         0.80899739,  1.03481896, -0.65956311,  1.22488398,  0.25171685],\n",
       "       [ 0.24628963, -0.49941338, -0.82799632, -2.49084714,  0.01814502,\n",
       "         0.56864766,  0.73362894, -0.82071924, -0.54472099, -0.29332133],\n",
       "       [ 0.19687903,  0.02123125,  1.10933436, -0.2687382 ,  0.08835836,\n",
       "         0.80899739,  1.21553297, -0.49840699,  2.13596773,  0.26901965],\n",
       "       [ 1.69154964, -0.34681064,  0.4879264 , -0.80925118,  0.93091845,\n",
       "         2.49144552,  1.46652465, -0.98187536,  1.03215473,  1.18606801],\n",
       "       [ 0.29570023,  0.22769377,  1.84040254,  0.45194578,  1.28198515,\n",
       "         0.80899739,  0.66335127,  0.22679555,  0.40140444, -0.31927553],\n",
       "       [ 1.48155459, -0.51736664,  0.30515936, -1.28970717,  0.86070511,\n",
       "         1.56209322,  1.36612798, -0.17609475,  0.66421706,  0.73186953],\n",
       "       [ 1.71625494, -0.4186237 ,  0.30515936, -1.46987817, -0.26270834,\n",
       "         0.32829793,  0.49267693, -0.49840699,  0.6817379 ,  0.08301456],\n",
       "       [ 1.3086175 , -0.16727801,  0.89001391, -0.56902319,  1.49262517,\n",
       "         0.48853108,  0.48263726, -0.41782893, -0.59728351, -0.00349944],\n",
       "       [ 2.25977152, -0.62508622, -0.7183361 , -1.65004916, -0.192495  ,\n",
       "         0.80899739,  0.95450162, -0.57898505,  0.6817379 ,  0.06138606],\n",
       "       [ 1.0615645 , -0.88540853, -0.352802  , -1.04947918, -0.12228166,\n",
       "         1.09741707,  1.12517596, -1.14303148,  0.45396697,  0.93517742]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_std[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "e276a1df",
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = PCA(n_components=0.95)\n",
    "data_pca = pca.fit_transform(data_std)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b0f4f3e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.n_components_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "76c3469c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2.76934698, -1.30493533, -0.58395762,  0.88718074, -0.05383194,\n",
       "         0.4175187 , -0.65310465, -0.55835533],\n",
       "       [ 1.67070079,  1.02905763, -1.81994227,  0.19988232, -0.34183924,\n",
       "        -0.70990577, -0.82282074,  0.70308247],\n",
       "       [ 2.10783949, -0.8316145 ,  0.99444906, -0.74968877,  0.52989459,\n",
       "         0.60452126,  0.11621797, -0.05362278],\n",
       "       [ 3.44931605, -1.8592707 , -0.38935836, -0.49400668, -0.16692107,\n",
       "        -0.15436675,  0.06916937,  0.40293675],\n",
       "       [ 0.87603192, -1.50882534,  1.63982782,  0.41881807, -0.08648593,\n",
       "        -0.29583018, -0.86454971, -0.03449526],\n",
       "       [ 2.67119513, -1.29513525, -0.69468732, -0.34175964, -0.58811531,\n",
       "         0.06150361, -0.65042096,  0.16782641],\n",
       "       [ 1.66955982, -0.57093005, -1.18079406, -0.5974367 , -0.40172288,\n",
       "        -0.36573743, -0.31139999, -0.76851304],\n",
       "       [ 1.13000447, -1.39071555, -0.27443572,  1.01082913, -0.5247762 ,\n",
       "        -0.92440137, -0.76028542, -0.21809656],\n",
       "       [ 2.39875256, -0.27899794, -1.83738049, -0.78097768, -0.38129604,\n",
       "        -0.29512257, -0.33663005, -0.9884012 ],\n",
       "       [ 2.3741835 , -0.34132632, -1.10598146, -0.4471483 , -0.49859661,\n",
       "        -0.22349767,  0.67538623,  0.25544845]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_pca[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ed78911",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8963f642",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebfe0471",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1a8ac37",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b5a3b725",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dc830c1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92a2f292",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d60f39b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
